﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ShoppingCartDTO
/// </summary>
public class ShoppingCartDTO
{
    public string UserNameBought { get; set; } // username customer mua hang
    public List<CartDTO> ListCart { get; set; } // gio hang 
    public const string SessionListCart = "ListCart";
    public const string SessionUserName = "UserName";

    //Lay ra username hien tai khi dang nhap va chua dang nhap
    public string GetUserName( HttpContext context)
    {
        if (context.Session[SessionUserName] == null ) // moi mua chua co session nen = null
        {
            if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
            {
                //username dang nhap rui thi gan vao session
                context.Session[SessionUserName] = context.User.Identity.Name;
            }
            else // khi chua dang nhap gan 1 chuoi random sau khi dang nhap rui gan lai
            {
                Guid temp = Guid.NewGuid(); // tao 1 chuoi random gom 32 ky tu giong md5
                context.Session[SessionUserName] = temp.ToString(); 
            }
        }
        return context.Session[SessionUserName].ToString();
    }
    //lay ra danh sach cac san pham gan vao session
    public void GetListCartCurrent(HttpContext context)
    {
        if (context.Session[SessionListCart] != null)
        {
            ListCart = (List<CartDTO>)context.Session[SessionListCart];
        }
        else
        {
            ListCart = new List<CartDTO>();
        }
    }
    // lay ra gio hang dang mua
    public static  ShoppingCartDTO GetShoppingCartCurrent(HttpContext context)
    {
        var shoppingCart = new ShoppingCartDTO();
        shoppingCart.GetListCartCurrent(context);
        shoppingCart.UserNameBought =shoppingCart.GetUserName(context);
        return shoppingCart;
    }
    // them san pham
    public bool AddToCart(AlbumDTO albumBuy)
    {
        bool bCheck = false;
        //check co album nay chua , phai la username hien tai
        var album = ListCart.SingleOrDefault(a => a.AlbumID == albumBuy.AlbumID && a.UserNameBought == this.UserNameBought);
        if (album != null)
        {
            album.Count++; // da co trong gio hang nen tang len 1
            bCheck = true;
        }
        else // chua co trong gio hang
        {
            album = new CartDTO
            {
                AlbumID = albumBuy.AlbumID,
                Count = 1,//moi mua nen so luong = 1
                DateCreated = DateTime.Now,
                Price = albumBuy.Price,
                Title = albumBuy.Title,
                UserNameBought = this.UserNameBought
            };
            bCheck = true;
            ListCart.Add(album); // them vao gio hang
            HttpContext.Current.Session[SessionListCart] = ListCart;//cap nhat lai session
        }
        return bCheck;
    }
    // lay ra tong tien cac san pham
    public decimal GetTotalMoney()
    {
        decimal dTotalMoney = 0;
        foreach( var item in ListCart )
        {
            dTotalMoney += item.TotalMoney;
        }
        return dTotalMoney;
    }
    // remove count san pham
    public bool RemoveFromCart(int iAlbumID)
    {
        bool bCheck = false;
        var album = ListCart.SingleOrDefault(a => a.AlbumID == iAlbumID && a.UserNameBought == this.UserNameBought);
        if ( album != null)
        {
            if (album.Count > 1)
            {
                album.Count--;
                bCheck = true;
            }
            else
            {
                ListCart.Remove(album);
                bCheck = true;
            }
            HttpContext.Current.Session[SessionListCart] = ListCart;
        }
        return bCheck;
    }
    //remove 1 san pham
    public void RemoveOneAlbum(int iAlbumID)
    {
        var album = ListCart.SingleOrDefault(a => a.AlbumID == iAlbumID && a.UserNameBought == this.UserNameBought);
        if ( album != null )
        {
            ListCart.Remove(album);
        }
    }
    //update san pham ve so luong
    public bool UpdateCart(int iAlbumID, int iCount)
    {
        bool bCheck = false;
        var album = ListCart.SingleOrDefault(a => a.AlbumID == iAlbumID && a.UserNameBought == this.UserNameBought);
        if (album != null )
        {
            if (iCount > 0)
            {
                album.Count = iCount;
                bCheck = true;
            }
            else
            {
                RemoveOneAlbum(iAlbumID);
                bCheck = true;
            }
        }
        HttpContext.Current.Session[SessionListCart] = ListCart;
        return bCheck;
    }
}